﻿using SharpErp.Data.Common.Context;
using SharpErp.UI.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SharpErp.UI.BasicData
{
    public partial class PathDetailDialog : UI.Common.BaseDialogForm
    {
        public Data.Common.Entity.LocalSettingItem Item { get; set; }

        public int SettingTag { get; set; } = LocalSettingContext.LOCAL_SETTING_TAG_DEFAULT;

        public OperationType OperType { get; set; }

        public PathDetailDialog()
        {
            InitializeComponent();
            AfterLoad += PathDetailDialog_AfterLoad;
        }

        private void PathDetailDialog_AfterLoad(object sender, EventArgs e)
        {
            var drivers = Directory.GetLogicalDrives();
            for(var c ='H'; c <= 'Z'; ++c)
            {
                if (drivers.Contains(c + ":\\")) continue;
                cmbLocalDriver.Items.Add(c + ":\\");
            }
            if(Item != null)
            {
                string[] items = Item.Value.Split(new string[] { "=>" }, StringSplitOptions.RemoveEmptyEntries);
                txtNetFolder.Text = items[0];
                cmbLocalDriver.Text = items[1];
                txtLoginName.Text = items.Length > 2 ? items[2] : "";
                txtPassword.Text = items.Length > 3 ? items[3] : "";
            }
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            if(sender == buttonOk)
            {
                if (string.IsNullOrEmpty(cmbLocalDriver.Text))
                {
                    UI.Common.ToastNotificationUtil.ShowErrorToast(this, "请选择本地路径！");
                    return;
                }
                if (string.IsNullOrEmpty(txtNetFolder.Text))
                {
                    UI.Common.ToastNotificationUtil.ShowErrorToast(this, "请输入网络路径！");
                    return;
                }
                var localPath = cmbLocalDriver.Text.EndsWith("\\") ?  cmbLocalDriver.Text.Substring(0, cmbLocalDriver.Text.Length - 1) : cmbLocalDriver.Text;
                SharpErp.Common.Util.DriveReflection.WNetDisconnectDrive(localPath, true);
                bool result = SharpErp.Common.Util.DriveReflection.WNetReflectDrive(localPath, txtNetFolder.Text, txtLoginName.Text, txtPassword.Text);
                if (!result)
                {
                    UI.Common.ToastNotificationUtil.ShowErrorToast(this, "网络驱动器映射失败！");
                    return;
                }
                UI.Common.ToastNotificationUtil.ShowSuccessToast(this, "网络映射成功！");
                switch (OperType)
                {
                    case OperationType.OpAdd:
                        using( var context = new Data.Common.Context.LocalSettingContext())
                        {
                            context.SaveLocalSettingItem(new Data.Common.Entity.LocalSettingItem {
                                Name = "共享网盘映射",
                                Value = txtNetFolder.Text + "=>" + cmbLocalDriver.Text + "=>" + txtLoginName.Text + "=>" + txtPassword.Text,
                                Tag = SettingTag,
                            });
                        }
                        DialogResult = DialogResult.OK;
                        break;
                    case OperationType.OpView:
                        DialogResult = DialogResult.Cancel;
                        break;
                    case OperationType.OpModify:
                        Item.Name = "共享网盘映射";
                        Item.Value = txtNetFolder.Text + "=>" + cmbLocalDriver.Text + "=>" + txtLoginName.Text + "=>" + txtPassword.Text;
                        using (var context = new Data.Common.Context.LocalSettingContext())
                        {
                            context.SaveLocalSettingItem(Item);
                        }
                        DialogResult = DialogResult.OK;
                        break;
                }
            }else
            {
                DialogResult = DialogResult.Cancel;
            }
            this.Close();
        }
    }
}
